import './AdDisablePatch'; // 广告禁用补丁 - 必须在最前面
import './KuaishouAdapter'; // 导入快手小游戏适配脚本
import { _decorator, Component, director, Node } from 'cc';
import { MainWork } from './MainWork';
import Util from './Util';
import *as cc from "cc";
import DatasSpace from './Tool/DatasSpace';
import SkdComm from './SkdComm';
import { EVENT_TYPE, MISSION_TYPE } from '../Cube/Levels';
import { AudioMgr, ENUM_CLIP } from './Tool/AudioMgr';
import { TiliMgr } from './Tool/TiliMgr';
import EventMgr from './Tool/EventMgr';
import GlobeData from './GlobeData';
const { ccclass, property } = _decorator;

@ccclass('Home')
export class Home extends MainWork {

  @property(cc.Node)
  btnGameClub: cc.Node = null;

  @property(cc.Label)
  goldNumLabel: cc.Label = null;

  @property(cc.Label)
  ftNumLabel: cc.Label = null;

  @property(cc.Label)
  curretLevelLabel: cc.Label = null;

  @property(cc.ProgressBar)
  homeBox_starProgress: cc.ProgressBar = null;

  @property(cc.ProgressBar)
  homeBox_levelPassProgress: cc.ProgressBar = null;

  @property(cc.Label)
  freeCountLabel:cc.Label=null;
  @property(Node)
  shareIcon:Node=null;
  @property(Node)
  videoIcon:Node=null;

  static inst: Home = null;
  start() {
    Home.inst = this;
    this.checkUserGoldNumAndFtNum();
    this.checkHomeBoxInfo();
    EventMgr.on(EVENT_TYPE.EVENT_UPD_TILI, this.checkUserGoldNumAndFtNum, this)

    this.curretLevelLabel.string = '第' + (DatasSpace.Instance.level + 1) + '关';
    // this.popReward(1);
    SkdComm.Instance.createGameClub(this.btnGameClub);
    // Util.Instance.autoShowInsterAd(); // 暂时禁用自动插屏广告
    this.showStartBtn();
    console.log("开始");
  }

  showStartBtn(){
    if(GlobeData.Instance.freeCount>0){
      this.freeCountLabel.node.parent.position.set(0,27);
      this.shareIcon.active=false;
      this.videoIcon.active=false;
    }else{
      this.freeCountLabel.node.parent.position.set(30,27);
      if(GlobeData.Instance.startType==1){
        this.shareIcon.active=true;
        this.videoIcon.active=false;
      }else{
        this.shareIcon.active=false;
        this.videoIcon.active=true;
      }
    }
    this.freeCountLabel.string=`今日次数(${GlobeData.Instance.freeCount}/3)`;
  }

  protected onDestroy(): void {
    EventMgr.off(EVENT_TYPE.EVENT_UPD_TILI, this.checkUserGoldNumAndFtNum, this)
  }

  protected onDisable(): void {
    SkdComm.Instance.hideGameClub();
  }



  /**按钮监听*/
  BtnCODE(event, customEventData) {
    AudioMgr.Instance.play(ENUM_CLIP.Click);
    var btnName = event.target.name;
    switch (btnName) {
      case "btnStartMainGame":
        this.startMainGame();
        break;

      case 'btnSetting':
        this.openSetting();
        break;

      case 'btnPropShop':
        this.openPropShop();
        break;

      case 'btnGoldAdd':
        this.openGoldAdd();
        break;

      case 'btnFatigueAdd':
        this.openFtAdd();
        break;

      case 'btnLuckilyBox':
        this.openLuckilyBox();
        break;

      case 'btnRank':
        this.openRank();
        break;

      case 'btnInvateFriend':
        //this.openInvateFriend();
        this.openDailyShare();
        break;

      case 'btnShare':
        this.openToShare();
        break;

      case 'btnSaveGold':
        this.openSaveGold();
        break;

      case 'btnDailyChallenge':
        this.openDailyChallenge();
        break;

      case 'btnHomeBoxLevel':
        this.openHomeBox(event, 2);
        break;

      case 'btnHomeBoxStar':
        this.openHomeBox(event, 1);
        break;

      case 'btnMission':
        this.openMission();
        break;
    }

  }

  //_________________________________________________________________________________________________________________________________________________________
  /**开始游戏*/
  isStart:boolean=false;
  startMainGame() {
    if(this.isStart){
      return;
    }
    this.isStart=true;
    if (GlobeData.Instance.freeCount> 0) {
      GlobeData.Instance.freeCount -=1;
      GlobeData.Instance.setFreeCount();
      Util.Instance.mainGameLevelMode = 1;
      director.loadScene('MainGame');
    } else {
      Util.Instance.mainGameLevelMode = 1;
      if(GlobeData.Instance.startType==1){//分享
        GlobeData.Instance.startType=2;
        GlobeData.Instance.setStartType();
        Util.Instance.share();
        setTimeout(()=>{
          director.loadScene('MainGame');
        },3000);
      } else if(GlobeData.Instance.startType==2){//看视频
        GlobeData.Instance.startType=1;
        GlobeData.Instance.setStartType();
        console.log('播放激励视频广告');
        Util.Instance.VideoPlay("start",res=>{
          if(res){
            director.loadScene('MainGame');
          }else{
            console.log("广告播放失败");
          }
        })
      }
      //this.openFtAdd();
    }

  }

  /**打开设置 */
  openSetting() {
    Util.Instance.GloberLoadPrefab('Setting', 1, (res) => {
      let newNode = cc.instantiate(res);
      this.node.addChild(newNode);
    }, () => {

    });
  }

  //============================================================================5月28日=========================================================================
  //增加 时间  2024年 5月  28日
  /**打开道具商店 */
  openPropShop() {
    Util.Instance.GloberLoadPrefab('PropShop', 1, (res) => {
      let newNode = cc.instantiate(res);
      this.node.addChild(newNode);
    }, () => {

    });
  }


  /**打开金币增加弹窗
   * time 2024 5 29 1:04
   */
  openGoldAdd() {
    Util.Instance.GloberLoadPrefab('GoldAddAndFtAdd', 1, (res) => {
      let newNode = cc.instantiate(res);
      newNode.getComponent('GoldAddAndFtAdd').initViewShow(1);
      this.node.addChild(newNode);
    }, () => {

    });
  }

  /**
   * 检查用户金币和体力值
   * time 2024 5 29 1:12
   */
  checkUserGoldNumAndFtNum() {
    this.goldNumLabel.string = DatasSpace.Instance.goldNum + '';
    this.ftNumLabel.string = DatasSpace.Instance.ftNum + '';
  }

  /**
   * 打开体力增加弹窗 
   * time 2024 5 29 1:27
  */
  openFtAdd() {
    Util.Instance.GloberLoadPrefab('GoldAddAndFtAdd', 1, (res) => {
      let newNode = cc.instantiate(res);
      newNode.getComponent('GoldAddAndFtAdd').initViewShow(2);
      this.node.addChild(newNode);
    }, () => {

    });
  }

  /**
   * 打开幸运宝箱
   * time 2024 5 29 1:53
   */
  openLuckilyBox() {
    Util.Instance.GloberLoadPrefab('LuckBox', 1, (res) => {
      let newNode = cc.instantiate(res);
      this.node.addChild(newNode);
    }, () => {

    });

  }

  /**
   * @fn 打开排行榜
   * @time 2024 5 29 13:44
   */
  openRank() {
    Util.Instance.GloberLoadPrefab('Rank', 1, (res) => {
      let newNode = cc.instantiate(res);
      this.node.addChild(newNode);
    }, () => {

    });
  }

  /**
   * @fn 呼朋唤友 邀请好友
   * @time 2024 5 29 13:49
   */
  openInvateFriend() {
    SkdComm.Instance.shareTo();
  }

  /**
   * @fn 分享游戏
   * @time 2024 5 29 13:50
   */
  openToShare() {
    SkdComm.Instance.shareTo();
  }

  /**
   * @fn 打开存钱罐
   * @time 2024 5 29 15:21
   */
  openSaveGold() {
    Util.Instance.GloberLoadPrefab('PiggyBank', 1, (res) => {
      let newNode = cc.instantiate(res);
      this.node.addChild(newNode);
    }, () => {

    });
  }

  /**
   * @fn 打开任务奖励
   * @time 2024 5 29 16:19
   */
  openMission() {
    let newNode = this.node.getChildByName('PropMission');
    if (!newNode) {
      Util.Instance.GloberLoadPrefab('PropMission', 1, (res) => {
        newNode = cc.instantiate(res);
        this.node.addChild(newNode);
      }, () => {

      });
    } else {
      newNode.active = true;
    }
  }

  openDailyShare() {
    Util.Instance.GloberLoadPrefab('PropShare', 1, (res) => {
      let newNode = cc.instantiate(res);
      this.node.addChild(newNode);
    }, () => {

    });
  }

  /**
   * @fn 打开每日挑战
   * @time 2024 5 29 16:19
   */
  openDailyChallenge() {
    Util.Instance.GloberLoadPrefab('DailyChanllge', 1, (res) => {
      let newNode = cc.instantiate(res);
      this.node.addChild(newNode);
    }, () => {

    });
  }

  /**
   * @fn 检查首页宝箱数量信息
   * @time 2024 5 30 2:41
   */
  checkHomeBoxInfo() {
    this.homeBox_starProgress.progress = Math.floor(DatasSpace.Instance.starNum / 500 * 100) / 100;
    cc.find('Canvas/btnHomeBoxStar/starNum').getComponent(cc.Label).string = DatasSpace.Instance.starNum + '/500';
    this.homeBox_levelPassProgress.progress = DatasSpace.Instance.homeBox_levelPassCountNum / 5;
    cc.find('Canvas/btnHomeBoxLevel/levelNum').getComponent(cc.Label).string = DatasSpace.Instance.homeBox_levelPassCountNum + '/5';
  }

  /**
   * @fn 打开首页宝箱
   * @time 2024 5 30 2:50
   */
  openHomeBox(event, type) {
    let btnNode: Node = event.target;
    let tipsNode = btnNode.getChildByName('tips');
    tipsNode.active = true;
    cc.tween(tipsNode)
      .delay(2)
      .set({ active: false })
      .start();

    if (type == 1) {
      console.log('打开宝箱1');
      if (DatasSpace.Instance.starNum >= 500) {
        //奖励100金币和一个加时器
        DatasSpace.Instance.starNum = 0;
        DatasSpace.Instance.setStarNum();
        DatasSpace.Instance.propNumArr[4] += 2;
        DatasSpace.Instance.setPropNum();
        DatasSpace.Instance.goldNum += 100;
        DatasSpace.Instance.setGoldNum();
        DatasSpace.Instance.addMission_Target(MISSION_TYPE.STARBOX_GET_COUNT, 1)

        this.checkHomeBoxInfo();
        this.popReward(1);
      } else {
        console.log('星星宝箱未存满');
      }
    } else if (type == 2) {
      console.log('打开宝箱2');
      if (DatasSpace.Instance.homeBox_levelPassCountNum >= 5) {
        //奖励100金币和一个加时器
        DatasSpace.Instance.homeBox_levelPassCountNum = 0;
        DatasSpace.Instance.setHomeBoxLevelPassCount();
        DatasSpace.Instance.propNumArr[4] += 2;
        DatasSpace.Instance.setPropNum();
        DatasSpace.Instance.goldNum += 100;
        DatasSpace.Instance.setGoldNum();
        DatasSpace.Instance.addMission_Target(MISSION_TYPE.LEVELBOX_GET_COUNT, 1)

        this.checkHomeBoxInfo();
        this.popReward(1);
      } else {
        console.log('关卡宝箱未存满');
      }
    }

  }

  /**
   * @fn 奖励获得弹窗
   * @time 2024 5 30 12:20
   */
  popReward(rewardType, rewardNums = 0) {
    Util.Instance.GloberLoadPrefab('PopReward', 1, (res) => {
      let newNode = cc.instantiate(res);
      newNode.getChildByName('View').getChildByName('Type' + rewardType).active = true;
      this.node.addChild(newNode);
      AudioMgr.Instance.play(ENUM_CLIP.Achievement);
    }, () => {

    });
  }

}


